﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;

namespace MovieTweet
{
	public class Ratings
	{
		public string critics_score;
		public string audience_score;
	}

	public class Person
	{
		public string name;
	}

	public class Posters
	{
		public string detailed;
	}

	public class RottenResult
	{
		public string id;
		public string title;
		public string year;
		public string runtime;
		public Ratings ratings;
		public List<string> genres;
		public string synopsis;
		public List<Person> abridged_cast;
		public List<Person> abridged_directors;
		public Posters posters;

	}
	public class Links
	{
		public string next;
	}
	public class RottenJsonObject
	{
		public List<RottenResult> movies;
		public Links links;
	}

	public class RottenMovieID
	{
		public string id;
		public string name;
	}

	class RottenInfo
	{
		public RottenInfo(string source)
		{
			switch (source)
			{
				case ("In theaters"):
					baseUrl = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/in_theaters.json?page_limit=50";
					break;

				case ("Upcoming"):
					baseUrl = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/upcoming.json?page_limit=50";
					break;

				case ("DVD"):
					baseUrl = "http://api.rottentomatoes.com/api/public/v1.0/lists/dvds/current_releases.json?page_limit=50";
					break;

				case ("Movie"):
					baseUrl = "http://api.rottentomatoes.com/api/public/v1.0/movies/";
					break;

				default: break;
			}
		}

		public static string baseUrl;
		public static string apikey = "apikey=53mh5h272f8g66vzhhsyb7fa";

		public string getJsonData(string url)
		{
			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
			request.Method = "GET";
			WebResponse response = request.GetResponse();
			StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
			return reader.ReadToEnd();
		}

		public RottenResult getMovie(string id)
		{
			string jsonData = getJsonData(baseUrl + id + ".json?" + apikey);
			JavaScriptSerializer jSerialize = new JavaScriptSerializer();
			RottenResult rottenResult = jSerialize.Deserialize<RottenResult>(jsonData);
			return rottenResult;
		}

		public List<RottenMovieID> getList()
		{
			string jsonData = getJsonData(baseUrl + "&" + apikey);
			JavaScriptSerializer jSerialize = new JavaScriptSerializer();
			RottenJsonObject rottenJsonObject = jSerialize.Deserialize<RottenJsonObject>(jsonData);
			List<RottenMovieID> movieIdList = new List<RottenMovieID>();

			while (true)
			{
				if (rottenJsonObject.links.next != null)
				{
					foreach (RottenResult movie in rottenJsonObject.movies)
					{
						RottenMovieID mid = new RottenMovieID();
						mid.id = movie.id;
						mid.name = movie.title;
						movieIdList.Add(mid);
					}
					jsonData = getJsonData(rottenJsonObject.links.next + "&" + apikey);
					rottenJsonObject = jSerialize.Deserialize<RottenJsonObject>(jsonData);
				}
				else
				{
					foreach (RottenResult movie in rottenJsonObject.movies)
					{
						RottenMovieID mid = new RottenMovieID();
						mid.id = movie.id;
						mid.name = movie.title;
						movieIdList.Add(mid);
					}
					break;
				}
			}

			return movieIdList;
		}
	}

}
